﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Jy123.Web.Manage
{
    public partial class Left : MgConfig
    {
        protected string LeftHtml = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("#");
            LeftHtml = GetGroup(mRight.FMPage);
        }
        private DataTable GetLeft(string right)
        {
            string rightstr = string.Empty;
            if (right != "#")
            {
                rightstr = " and id in({0})";
                rightstr = string.Format(rightstr, right);
            }
            string sqlStr = "SELECT (SELECT pName FROM Manage_Page a WHERE ID = b.upid) upName,pName,url FROM Manage_Page b WHERE upid<>0{0} GROUP BY upid,pname,url ORDER BY upid DESC";
            return DBUtility.DbHelperSQL.Query(string.Format(sqlStr, rightstr)).Tables[0];
        }
        private string GetGroup(string right)
        {
            string hBox = @"
            <h3><a href=""#"">{0}</a></h3>
            <div>
                <ul>{1}
                </ul>
            </div>";
            string hItems = @"
                    <li><a href=""{0}"" target=""main"">{1}</a></li>";
            StringBuilder strGroup = new StringBuilder();
            StringBuilder strPage = new StringBuilder();
            DataTable dt = GetLeft(right);
            if (dt.Rows.Count > 0)
            {
                string pName = dt.Rows[0][0].ToString();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (pName != dt.Rows[i][0].ToString())
                    {
                        strGroup.AppendFormat(hBox, pName, strPage.ToString());
                        pName = dt.Rows[i][0].ToString();
                        strPage.Remove(0, strPage.Length);
                    }
                    strPage.AppendFormat(hItems, dt.Rows[i][2], dt.Rows[i][1]);
                    if (i + 1 == dt.Rows.Count)
                    {
                        strGroup.AppendFormat(hBox, pName, strPage.ToString());
                    }
                }
            }
            return strGroup.ToString();
        }
    }
}
